//
//  PicCollectionView.swift
//  BQWB
//
//  Created by laibingqiang on 16/10/28.
//  Copyright © 2016年 laibingqiang. All rights reserved.
//

import UIKit
import SDWebImage
//item的宽高
// cell 间距
private let cellMargin: CGFloat = 5
// cell 宽高
private let cellWH = CGFloat(Int((BQSCREENW - 20 - cellMargin*2)/3))
class PicCollectionView: UICollectionView {

    
    var url_pics:[HomePicModel]?{
        didSet{
            
            setupConstranints(count: (url_pics?.count)!)
            self.reloadData()
        }
    }
    //设置配图信息的主方法
    private func setupConstranints(count:Int){
        
        if count == 1{
            let key = url_pics?.last?.thumbnail_pic
            
            let img = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: key)
            if img != nil{
                
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                layout.itemSize = (img?.size)!
                self.snp_updateConstraints(closure: { (make) in
                    make.size.equalTo((img?.size)!)
                })
                layoutIfNeeded()
                return
            }
        }
        let col = count >= 3 ? 3 : count
        let row = (count - 1) / 3 + 1
        
        let w = cellWH * CGFloat(col) + cellMargin * CGFloat(col - 1)
        
        let h = cellWH * CGFloat(row) + cellMargin * CGFloat(row - 1)
        
        self.snp_updateConstraints { (make) in
            make.size.equalTo(CGSize(width: w, height: h))
        }
        layoutIfNeeded()
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        
    }
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置视图
    private func setupUI(){
        
        self.backgroundColor = UIColor(white: 237/256, alpha: 1)
        self.dataSource = self
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        
        
        
        //间距
        layout.minimumInteritemSpacing = cellMargin
        
        layout.minimumLineSpacing = cellMargin
        
        //self.frame = CGRect(x: 0, y: 0, width: 375, height: 500)
        
        self.register(HomePicCell.self, forCellWithReuseIdentifier: "pic_cell")
    }
}


//数据源方法
extension PicCollectionView:UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (url_pics?.count) ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "pic_cell", for: indexPath) as! HomePicCell
        
        //cell.backgroundColor = UIColor.red
        
        cell.pic_url = url_pics?[indexPath.item]
        
        return cell
    }
}
